package DHBW.BT;

/**
 * This class is a abstract container that store the received bluetooth data
 * from the pedelec. No changes to the data is made nor any declaration what
 * is behind the data
 * @author baumlndn
 *
 */
public class BluetoothFrame {
	/**
	 * MAX_BT_ID defines the maximum amount of messages
	 */
	protected static final int MAX_BT_ID = 32;
	/**
	 * MAX_BT_LENGTH defines the maximum length of a message
	 */
	protected static final int MAX_BT_LENGTH = 7;
	
	/**
	 * data contains the message data saved in the BluetoothFrame
	 */
	protected byte[][] data =  new byte[MAX_BT_ID][MAX_BT_LENGTH];
	
	/**
	 * Default constructor clears the array
	 */
	public BluetoothFrame() {
		clearData();
	}
	
	/**
	 * Constructor
	 * @param temp_data data to be filled into the array
	 */
	public BluetoothFrame(byte[][] temp_data) {
		data = temp_data;
	}
	
	/**
	 * Fills the array with 0
	 */
	public void clearData() {
		for (int i=0;i<MAX_BT_ID;i++) {
			for (int j=0;j<MAX_BT_LENGTH;j++) {
				data[i][j] = 0;
			}
		}
	}
	
	/**
	 * Writes a value into the array
	 * @param temp_id ID of the data
	 * @param temp_length length of the data (1-7)
	 * @param temp_data byte-array of data
	 */
	public void setData(int temp_id, int temp_length, byte[] temp_data) {
		if (temp_id <= MAX_BT_ID && temp_length <= MAX_BT_LENGTH) {
			for (int i=0;i<temp_length;i++) {
				data[temp_id][i] = temp_data[i];
			}
		}
	}

	/**
	 * Function to read data back from the array
	 * @param temp_id ID of the requested data
	 * @param temp_length length of data requested
	 * @return byte-array from the internal array
	 */
	public byte[] getData(int temp_id, int temp_length) {
		byte[] temp_result = new byte[temp_length];
		
		for (int i=0;i<temp_length;i++) {
			temp_result[i] = data[temp_id][i];
		}
		
		return temp_result;
	}
	
	public void importData(BluetoothFrame temp_frame) {
		for (int i=0;i<MAX_BT_ID;i++) {
			for (int j=0;j<MAX_BT_LENGTH;j++) {
				data[i][j] = temp_frame.data[i][j];
			}
		}
	}
	
}
